﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using VideoPlay.Services.Models;

namespace VideoPlay.Adapters
{
    public class FavoriteFacade 
    {
        private static string CONNECTION_STRING = @"isostore:/VideoPlay.sdf";

        public static void Save(List video)
        {
            using (LocalDataBaseContext context = new LocalDataBaseContext(CONNECTION_STRING))
            {
                List videoToInsert = new List();
                videoToInsert.lcLocalId = video.lcLocalId;
                videoToInsert.lcYoutubeId = video.lcYoutubeId;
                videoToInsert.image = video.image;
                videoToInsert.title = video.title;
                videoToInsert.lcDuration = video.duration.ToString() ;
                videoToInsert.url = video.url;

                context.Lists.InsertOnSubmit(videoToInsert);

                context.SubmitChanges();
            }
        }

        public static void Remove(List video)
        {
            using (LocalDataBaseContext context = new LocalDataBaseContext(CONNECTION_STRING))
            {
                IQueryable<List> videoQuery = from v in context.Lists
                                              where (v.lcLocalId == video.lcLocalId && v.lcYoutubeId == null) ||
                                                     (v.lcLocalId == 0 && v.lcYoutubeId == video.lcYoutubeId) ||
                                                     (v.lcLocalId == video.lcLocalId && v.lcYoutubeId == video.lcYoutubeId)
                                               select v;
                List videoToDelete = videoQuery.FirstOrDefault();

                context.Lists.DeleteOnSubmit(videoToDelete);

                context.SubmitChanges();
            }
        }

        public static List GetVideoById(int id,string youtubeId)
        {
            using (LocalDataBaseContext context = new LocalDataBaseContext(CONNECTION_STRING))
            {
                IQueryable<List> videoQuery = from v in context.Lists
                                              where (v.lcLocalId == id && v.lcYoutubeId == null) ||
                                                     (v.lcLocalId == 0 && v.lcYoutubeId == youtubeId) ||
                                                     (v.lcLocalId == id && v.lcYoutubeId == youtubeId)
                                              select v;
                List video = videoQuery.FirstOrDefault();

                context.SubmitChanges();

                return video;
            }
        }

        public static List<List> GetPage(int skip, int numberOfRow)
        {
            using (LocalDataBaseContext context = new LocalDataBaseContext(CONNECTION_STRING))
            {
                IQueryable<List> videoQuery = (from v in context.Lists
                                               select v)
                                               .Skip(skip)
                                               .Take(numberOfRow);
                return videoQuery.ToList();
            }
        }
    }
}
